var form = $("form:eq(0)");
var progress = $(".progress");
form.on("submit", function() {
	let formData = new FormData(form[0]);

	progress.show();

	$.ajax({
		type: "post",
		url: "food/foodgoods/add.do",
		processData: false, // 不会将 data 参数序列化字符串
		contentType: false, // 根据表单 input 提交的数据使用其默认的 contentType
		data: formData,
		success: function(data) {
			if(data.ok) {
				toastr.success("添加成功");
				form.trigger("reset");
			} else {
				toastr.error(data.message);
			}
			progress.hide();
		},
		dataType: "json",
		xhr: function() {
			var xhr = new window.XMLHttpRequest();
			xhr.upload.addEventListener("progress", function(evt) {
				if(evt.lengthComputable) {
					var percentComplete = evt.loaded * 100 / evt.total;
					progress.children().css("width", percentComplete + "%");
				}
			}, false);
			return xhr;
		}
	});

	return false;
});

form.find("._img").on("click", function() {
	$(this).prev().trigger("click");
	return false;
});

var imgFile = form.find(":file[name='file']");
var img = $("#imgShow");

imgFile.on("change", function() {
	var f = imgFile[0].files[0];
	console.info(f);
	var reader = new FileReader();
	reader.onload = function() {
		//		alert("aa");
		img[0].src = reader.result;
	}
	reader.readAsDataURL(f);
});
form.on("reset", function() {
	img[0].src = "/imgs/_noimg.png";
});

$("._gis").on("click",function(){
	$("#gis").toggle();
});

window.frames.gis.addEventListener("map-select",function(e){
	$("._lng").val(e.data.lng);
	$("._lat").val(e.data.lat);
});